"""
河南省可视化地图绘制
"""

import json
from pyecharts.charts import Map
from  pyecharts.options import *
# 读取数据文件
file_name = "D:/python code/1、Python快速入门（8天零基础入门到精通）/第1-12章资料/可视化案例数据/地图数据/疫情.txt"
f = open(file_name,"r",encoding="UTF-8")
data = f.read()

# 将JSON转为python数据
my_dict = json.loads(data)

# 关闭文件
f.close()

# 获取河南省的数据
children_data = my_dict['areaTree'][0]['children'][3]['children']

# 获取河南省内各地区的数据
data_list = []
for region_data in children_data:
    region_name = region_data['name']+"市"
    region_confirm = region_data['total']['confirm']
    data_list.append((region_name,region_confirm))

# 手动添加济源市
data_list.append(("济源市",100))
# 获得地图对象
map = Map()

# 添加数据
map.add("河南省疫情地图",data_list,"河南")

# 设置全局变量
map.set_global_opts(
    title_opts=TitleOpts(title="河南疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,  # 是否分段
        pieces=[
            {"min": 1, "max": 99, "label": "1-99人", "color": "#CCFFFF"},
            {"min": 100, "max": 999, "label": "100-999人", "color": "#FFFF99"},
            {"min": 1000, "max": 4999, "label": "1000-4999人", "color": "#FF9966"},
            {"min": 5000, "max": 9999, "label": "5000-9999人", "color": "#FF6666"},
            {"min": 10000, "max": 99999, "label": "10000-99999人", "color": "#CC3333"},
            {"min": 100000, "label": "100000+", "color": "#990033"}
        ]
    )
)

# 绘制地图
map.render("河南省疫情地图.html")